<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
/**
 * Emails Controller
 *
 * @property Email $Email
 * @property PaginatorComponent $Paginator
 */
class EmailsController extends AppController {
public $helpers = array('Html', 'Form','Session');		
		
/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	
	public function index() {

		$this->Paginator->settings = $this->paginate;
		$this->set('emails', $this->Paginator->paginate());

		//$this->Email->recursive = 0;
		//$this->set('emails', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->loadModel('CustomersEmail');
		if (!$this->Email->exists($id)) {
			throw new NotFoundException(__('Invalid email'));
		}
		else{
			$options = array('conditions' => array('Email.' . $this->Email->primaryKey => $id));
			$this->set('email', $this->Email->find('first', $options));

			$options = array('conditions' => array('CustomersEmail.email_id'=> $id), 'recursive' => 2);
			$this->set('sendtogroup', $this->Email->CustomersEmail->find('all', $options));
		}
		

		// $this->theme ='';
		// $this->layout='';
	}

	public function sentemail($sentto)
	{	
		 $Email = new CakeEmail('gmail');
	      $Email->from(array('thientruc92@gmail.com' => 'My Site'));
	      $Email->to($sentto);
	      $Email->subject('About');
	      $Email->emailFormat('html');
	      $Email->template('email');
	      $Email->helpers(array('Html'));
	      $Email->viewVars(array('title'=>'Xin chao thien truc'));
	      $Email->send('My message');  
	}
/**
 * add method
 *
 * @return void
 */

	public function add() {
		$this->set('title_for_layout', 'Create new email');
		$list_group= $this->Email->Customer->Group->find('list');
		$this->set('list_group', $list_group);

		if ($this->request->is('post')) {
			$this->Email->create();
			if ($this->Email->save($this->request->data)) {
				$this->Session->setFlash(__('The email has been saved.'));
				// return $this->redirect(array('action' => 'index'));

			} else {
				$this->Session->setFlash(__('The email could not be saved. Please, try again.'));
			}
		}
		$customers = $this->Email->Customer->find('list');
		// $this->set(compact('customers'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Email->exists($id)) {
			throw new NotFoundException(__('Invalid email'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Email->save($this->request->data)) {
				$this->Session->setFlash(__('The email has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The email could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Email.' . $this->Email->primaryKey => $id));
			$this->request->data = $this->Email->find('first', $options);
		}
		$customers = $this->Email->Customer->find('list');
		$this->set(compact('customers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Email->id = $id;
		if (!$this->Email->exists()) {
			throw new NotFoundException(__('Invalid email'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Email->delete()) {
			$this->Session->setFlash(__('The email has been deleted.'));
		} else {
			$this->Session->setFlash(__('The email could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}


	
	//send email
	public function send(){
		$k= $_POST['email'];
		$this->Email->create();
			if ($this->Email->save(array('title'=>$k['title'], 'content'=> $k['content'] ))) {
				// $this->Session->setFlash(__('The email has been saved.'));
				$email_id= $this->Email->getLastInsertId();
			} else {
				// $this->Session->setFlash(__('The email could not be saved. Please, try again.'));
			}

			foreach($k['group'] as $key => $value) {
				 $list_customer_in_group = $this->Email->Customer->find('all',array(
				 	'conditions' => array('customer.group_id' => $value)
				 ));
				 // pr($list_customer_in_group);
				 foreach($list_customer_in_group as $k1 =>$v1){
				 	// pr($v1);die();
				 	// echo $v1['Customer']['id'];
				 	$this->Email->create();
				 	if ($this->Email->CustomersEmail->saveAll( array('email_id' => $email_id, 'customer_id' => $v1['Customer']['id'] ))) {
						$this->send_email_to_customer($v1['Customer']['id'], $v1['Customer']['email'], $k['title'], $k['content'],$this->Email->CustomersEmail->getLastInsertId());
						$this->Session->setFlash(__('The email has been send.'));
					} else {
						$this->Session->setFlash(__('The email could not be saved. Please, try again.'));
					}
				 }
			}


die();
	}

	//save temp email
	public function tempt_add() {
		if ($this->request->is('POST')) {
			
			$tempt_email = $_POST['form_data'];
			// pr($tempt_email);
			$this->set('tempt_email',$tempt_email);
			// $this->layout = 'ajax';
			die();
	}
	}

	//send email to groups customer
	public  function send_email_to_customer($customer_id, $list_customer, $title, $content,$cus_email_id){
		$Email = new CakeEmail('gmail');
			$Email->viewVars(array('customerid' => $customer_id,'CustomersEmail_id'=>$cus_email_id ,'content'=>$content));
			$Email->viewVars(array('title' => $title,'hosturl'=> 'http://'.$_SERVER['SERVER_NAME'].Router::url('/')));
			$Email->from(array('thientruc92@gmail.com' => 'Thien Truc practice cake'));
			$Email->to($list_customer);
			$Email->subject($title);
			$Email->emailFormat('html');
			$Email->template('email');
			$Email->send($content);
	}

	public function viewhistory(){
		$this->Paginator->settings = $this->paginate;
		$this->set('emails', $this->Paginator->paginate());

		// $email_records = $this->Email->find('all');
		// $this->set('emails', $email_records);
		// pr($email_records);die();
	}

	public function test()
	{
		$this->theme ='';
		$this->layout ='';
		$this->set('base_url', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
		
	}
}
